//============================================================================
// Name        : ROMS_main.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : C++ project Part II for your class (121, or 112) 
//============================================================================
//
#include "Simple_window.h"
#include "Graph.h"
#include <sstream>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Text_Display.H>

#include "ROMS_Menu.h"

using namespace std;
using namespace ROMS;
using namespace Graph_lib;

//types we need for a ROMS GUI interface-- a memu_bar and a place to display text and a means of attaching them to a Window.

namespace Graph_lib {
//------------------------------------------------------------------------------
	struct Window_text_display : Widget {
		Window_text_display(Point XY, int W, int H, string L, Callback cb): Widget(XY, W, H, L, cb)
		{}
		void set_buffer(string s);
		void attach(Window&);
	private:
		Fl_Text_Buffer data;
		Fl_Text_Display *ptd;

	};
	void Window_text_display::attach(Window& win)
	{
		ptd = new Fl_Text_Display(loc.x, loc.y, width, height, label.c_str());
		ptd->textfont(FL_COURIER);	//TNC B.2 change font face to make the alphabet fixed
		ptd->buffer(&data);
		pw  = (Fl_Widget*) ptd;
		pw->callback(reinterpret_cast<Fl_Callback*>(do_it), &win); // pass the window
		own = &win;
	}
	void Window_text_display::set_buffer(string s) 
	{
		data.text(s.c_str());
	}

//------------------------------------------------------------------------------
	struct Menu_bar : Widget {
		Menu_bar(Point XY, int W, int H, Callback cb): Widget(XY, W, H, "", cb)
		{}
		void menu(Fl_Menu_Item *ma);
		void attach(Window&);
	private:
		Fl_Menu_Bar *pmb;
		Fl_Menu_Item *ma;

	};
	void Menu_bar::attach(Window& win)
	{
		pmb = new Fl_Menu_Bar(loc.x, loc.y, width, height);
		pmb->menu(ma);
		pw  = (Fl_Widget*) pmb;
		pw->callback(reinterpret_cast<Fl_Callback*>(do_it), &win); // pass the window
		own = &win;
	}
	void Menu_bar::menu(Fl_Menu_Item *m) 
	{
		ma = m;
	}
}

//header declarations
void do_about_box(Window&);
void do_help_box(Window&); 	//[RAKA] B3
void do_read(Window&, ROMS_Menu&, string, string, Msg_type);
void Main_Window_CB(Fl_Widget*, void*);
void Menu_Bar_CB (Fl_Widget*, void*);
void Dialog_CB (Fl_Widget*, void*); //[RAKA] C

//[RAKA] C
void show(Window&, Window_text_display&, ROMS_Menu&);
void find(Window&, Window_text_display&, ROMS_Menu&);
void update(Window&, Window_text_display&, ROMS_Menu&);
void do_input_box(Window&, string);
void do_message_box(Window&, string);

//TNC D
void draw_graph(Window&, ROMS_Menu&);
void do_graph_box(Window&, string, ROMS_Menu&);

//global data
bool main_window_click = false;
bool menu_bar_click = false;
bool main_click_enabled = true;//used to force serialization on clicks
int window_userdata;

//[RAKA] C
int DialogValue 	= 0;
string DialogText 	= "";

//[RAKA] D
void create_bar_graph(Window&, double*, Graph_lib::Polygon*, int, int, int, int, int, int);

//main window menu bar
//Note: for expediancy sake, to define the menu bar below we used an FLTK type Fl_Menu_Item. 
//This makes our program  directly tied to FLTK instead of Dr. Stroustrup's interface Graph_lib.
//We don't like this, but we did it to save time, which often happens in the real world.
//A better solution is to write code to hide this in the implementation of the Graph_lib::Menu_bar, but that means adding
//changes to it and getting general agreement with Dr. Stroustrup. Subsequent testing and distribution would take time we don't have.
//An extra credit exercise is to see if you can eliminate direct references in your code to FLTK types like we have here
Fl_Menu_Item menu_bar[] = {
 {"Read", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Recipes", 0,  (Fl_Callback*)Main_Window_CB, Address (Read_recipes_ingredients), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Orders", 0,  (Fl_Callback*)Main_Window_CB, Address (Read_orders), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Menu Items", 0,  (Fl_Callback*)Main_Window_CB, Address (Read_category_menu_items), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 //[RAKA]B3
 {"Show", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Recipes", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_recipe), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Orders", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_order), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Categories", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_category), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Menu Items", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_menu), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Order Items", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_item), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Find", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Category Sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Find_category_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Table Sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Find_table_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Menu Item Sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Find_menu_item_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Update", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Add Order Item", 0,  (Fl_Callback*)Main_Window_CB, Address (Update_add_order_item), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Add Menu Item", 0,  (Fl_Callback*)Main_Window_CB, Address (Update_add_menu_item), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Add Recipe", 0,  (Fl_Callback*)Main_Window_CB, Address (Update_add_recipe), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Graph", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Order Sales by Month", 0,  (Fl_Callback*)Main_Window_CB, Address (Graph_order_month), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Total Sales by Table", 0,  (Fl_Callback*)Main_Window_CB, Address (Graph_total_table), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Total Category Sales by Month", 0,  (Fl_Callback*)Main_Window_CB, Address (Graph_category_month), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"About", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Info", 0,  (Fl_Callback*)Main_Window_CB, Address (About_info), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Help", 0,  (Fl_Callback*)Main_Window_CB, Address (About_help), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Exit", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Save and exit", 0,  (Fl_Callback*)Main_Window_CB, Address (Exit_program), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0}
};


//call back functions
void recps_cb(Address addr, Address) // callback for recipes button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Tables_button));
}
void order_cb(Address addr, Address) // callback for orders button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Orders_button));
}
void cat_cb(Address addr, Address) // callback for category button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Categories_button));
}
void mi_cb(Address addr, Address) // callback for menu item button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Menu_items_button));
}
void oi_cb(Address addr, Address) // callback for order item button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Recipes_button));
}
 void display_cb(Address addr, Address) // callback for display window
{
	Main_Window_CB((Fl_Widget*) addr, Address (Display_window));
}
 void menu_bar_cb(Address addr, Address)//callback for menu bar
 {
	 Menu_Bar_CB((Fl_Widget*) addr, Address (Menu_bar_cb));
 }
 
 //[RAKA] C
 //Callback for okay button
 void dialog_okay_cb(Address addr, Address) 
{
	Dialog_CB((Fl_Widget*) addr, Address (Dialog_okay));
}
 
 //[RAKA] C
 //Callback for cancel button
 void dialog_cancel_cb(Address addr, Address) 
{
	Dialog_CB((Fl_Widget*) addr, Address (Dialog_cancel));
}
 
 //[RAKA] C
//Dialog call back
void Dialog_CB (Fl_Widget*, void* ud) {
	//Destroy dialog
	menu_bar_click = true;
	
	//Save dialog choice
	window_userdata = (int) ud;
}

void Menu_Bar_CB (Fl_Widget*, void* ud) {
	cout << "Menu Bar Callback" << " Userdata=" << (int) ud << "\n";
	menu_bar_click = true;
}

void Main_Window_CB(Fl_Widget*, void* ud) {
	if(main_click_enabled)//don't signal the window click if main loop is active
	{
		cout << "Main Window Callback" << " Userdata=" << (int) ud << "\n";
		window_userdata = (int) ud;
		main_window_click = true;
		main_click_enabled = false; //main loop active on current click
	}
}

//wait functions for callbacks to be executed
void wait_for_main_window_click() {
	while (!main_window_click) 
		Fl::wait();
	main_window_click = false;
	Fl::redraw();
}

void wait_for_menu_bar_click() {
	while (!menu_bar_click)
		Fl::wait();
	menu_bar_click = false;
	Fl::redraw();
}

int main()
{
	//create the main window and display it
	ROMS_Menu m;
    Window sw(600, 500, "Restaurant Order Management System");
    Button recps(Point(40, 50), 100, 30, "Recipes", recps_cb);
	sw.attach(recps);
	Button order(Point(40, 100), 100, 30, "Orders", order_cb);
	sw.attach(order);
	Button cat(Point(40, 150), 100, 30, "Categories", cat_cb);
	sw.attach(cat);
	Button mi(Point(40, 200), 100, 30, "Menu Items", mi_cb);
	sw.attach(mi);
	Button ci(Point(40, 250), 100, 30, "Order Items", oi_cb);
	sw.attach(ci);
    sw.callback((Fl_Callback*)Main_Window_CB, Address (Exit_program));
	Menu_bar mb(Point(0, 0), 600, 30, menu_bar_cb);
    mb.menu(menu_bar);
	sw.attach(mb);

	Window_text_display t(Point(150,50),400, 400, "Data Display", display_cb );
	t.set_buffer("No Data Loaded\n");
	sw.attach(t);
 	sw.resizable(&sw);

	//main loop to execute user clicks on the main window
	//do you understand how this loop works compared to the way the main loop worked in Part I of the project?
	try {
		
		while(true) {
			main_click_enabled = true;
			wait_for_main_window_click();
			cout << "Act on userdata = " << window_userdata << endl;
			switch(window_userdata) 
			{
				//need to complete all the menu cases
				case Read_recipes_ingredients:
					do_read(sw, m, "recipes.dat", "Open Recipes/Ingredients File", Read_recipes_ingredients);
					break;
				case Read_orders:
					do_read(sw, m, "orders.dat", "Open Order/Order Items File", Read_orders);
					break;
				case Read_category_menu_items:
					do_read(sw, m, "catmenu.dat", "Open Category/Menu Item File", Read_category_menu_items);
					break;
				case About_info:
					do_about_box(sw);
					break;
				//[RAKA B3]
				case About_help:
					do_help_box(sw);
					break;
				//[RAKA B3]				
				case Show_category: case Show_item:
				case Tables_button: case Orders_button: case Categories_button: case Recipes_button: case Menu_items_button:
					t.set_buffer(m.show_button((Msg_type) window_userdata).c_str());
					break;

				case Show_order: case Show_recipe: case Show_menu:
					show(sw, t, m);
					break;
				
				//[RAKA]
				case Find_category_sales: case Find_table_sales: case Find_menu_item_sales:
					find(sw, t, m);
					break;
					
				//[RAKA]
				case Update_add_order_item: case Update_add_menu_item: case Update_add_recipe:
					update(sw, t, m);
					break;

				//TNC D
				case Graph_order_month: case Graph_total_table: case Graph_category_month:
					draw_graph(sw, m);
					break;

				case Exit_program:
					cout << "save files and exit\n";
					m.ROMS_Menu::save();//Josh B.1
					exit(0); //exit program
					break;
				default:
					cout << "case not implemented\n";
			}
		}
		return 0;
	}
	catch(exception& e) {
		cerr << "exception: " << e.what() << '\n';
		return 1;
	}
	catch (...) {
		cerr << "Some exception\n";
		return 2;
	}
}

//[RAKA] C
//Manage show menu
void show(Window& parent, Window_text_display& text, ROMS_Menu& menu)
{
	//Based on command
	switch(window_userdata) {
		case Show_order:	//TNC B.2
			text.set_buffer(menu.show_button((Msg_type) Orders_button).c_str());
			break;
		case Show_recipe:	//TNC B.2
			text.set_buffer(menu.show_button((Msg_type) Tables_button).c_str());
			break;
		case Show_menu:		//TNC B.2
			text.set_buffer(menu.show_button((Msg_type) Menu_items_button).c_str());
			break;
	}
}

//[RAKA] C
//Manage find menu
void find(Window& parent, Window_text_display& text, ROMS_Menu& menu)
{
	//Based on command
	switch(window_userdata) {
	case Find_category_sales:
		//Ask for category ID
		do_input_box(parent, "Category ID:");
		
		//Find sales if okay
		if (window_userdata == Dialog_okay) do_message_box(parent, menu.find_category_sales(DialogValue).c_str());
		break;
	
	case Find_table_sales: //JTL C
		do_input_box(parent, "Table ID:");
		if(window_userdata == Dialog_okay){
			do_message_box(parent, menu.find_table_sales(DialogValue));
		}
		break;
		
	case Find_menu_item_sales: //TNC C
		do_input_box(parent, "Menu Item ID:");
		if(window_userdata == Dialog_okay){
			do_message_box(parent, menu.find_menu_sales(DialogValue));
		}
		break;
	}
}

//[RAKA] C
//Manage update menu
void update(Window& parent, Window_text_display& text, ROMS_Menu& menu)
{
	//Based on command
	switch(window_userdata) {
		case Update_add_order_item: {//TNC C; 
			string newOrderItemID = "";				//New Order Item attribute
			int orderID, menuItemID, newQtyOrdered;	//New Order Item attribute
			stringstream resultMsg;					//Result message to be displayed (error / success)
			bool orderIDExist, menuItemIDExist;					//Temporary variable
			orderID = menuItemID = newQtyOrdered = 0;	//Initialization

			do_input_box(parent, "Order ID:");
			if(window_userdata == Dialog_okay){ orderID = DialogValue; do_input_box(parent, "Menu Item ID:");}
			if(window_userdata == Dialog_okay){ menuItemID = DialogValue; do_input_box(parent, "New Order Item ID:");}
			if(window_userdata == Dialog_okay) {newOrderItemID = DialogText; do_input_box(parent, "Menu Item Quantity:");}
			if(window_userdata == Dialog_okay) {newQtyOrdered = DialogValue;}
			
			orderIDExist		= menu.isOrderIDExist(orderID);
			menuItemIDExist		= menu.isMenuItemIDExist(menuItemID);
			if(!orderIDExist)	resultMsg << "Order ID " << orderID << " does not exist !!" << endl;
			if(!menuItemIDExist)	resultMsg << "Menu Item ID " << menuItemID << " does not exist !!";

			if(orderIDExist && menuItemIDExist)
				resultMsg << menu.add_order_item(orderID, menuItemID, newOrderItemID, newQtyOrdered);

			if(window_userdata == Dialog_okay)
				do_message_box(parent, resultMsg.str());

			break;
		}
	
		case Update_add_menu_item: {
			//Initialize
			int ID 			= 0;
			int Recipe		= 0;
			int Category	= 0;
			double Price	= 0;
			stringstream Name;
			stringstream Description;
		
			//Ask data
			do_input_box(parent, "Item ID:");
			if (window_userdata == Dialog_okay) {	ID			= DialogValue; 	do_input_box(parent, "Item name:");		}
			if (window_userdata == Dialog_okay) {	Name		<< DialogText; 	do_input_box(parent, "Description:");	}
			if (window_userdata == Dialog_okay) {	Description	<< DialogText; 	do_input_box(parent, "Category ID:");	}
			if (window_userdata == Dialog_okay) {	Category	= DialogValue; do_input_box(parent, "Recipe ID:");		}
			if (window_userdata == Dialog_okay) {	Recipe		= DialogValue; do_input_box(parent, "Price:");			}
			
			//If okay, add
			if (window_userdata == Dialog_okay) {
				Price = DialogValue;
				do_message_box(parent, menu.add_menu_item(ID, Category, Recipe, Name, Price, Description));
			}
			break;
		}
		
		case Update_add_recipe://JTL C
			int recipe_id = 0;
			stringstream Name;
			stringstream Instructions;
			
			do_input_box(parent, "Recipe ID:");
			if(window_userdata == Dialog_okay) { recipe_id = DialogValue; do_input_box(parent, "Recipe Name:");}
			if(window_userdata == Dialog_okay) { Name << DialogText; do_input_box(parent, "Instructions:");	}
			if (window_userdata == Dialog_okay) {
				Instructions << DialogText;
				do_message_box(parent, menu.add_recipe(recipe_id, Name, Instructions));
			}
			break;
		}
}

//show about box info window
void do_about_box(Window& w) 
{
		Window ab(Point(w.x()+100, w.y()+50), 800, 600, "About Box");
	ab.color(Color::white);
	ab.callback((Fl_Callback*)Menu_Bar_CB, Address (Close_about_box));
	Text msg(Point(15,30), "Ward Project Part II: Restaurant Order Management System");
	msg.set_color(Color::black);
	Text msg2(Point(15,50), "Team 10");//Josh B.1
	msg2.set_color(Color::black);
	Text msg3(Point(15,70), "Josh Latvatalo - TAMU");//Josh B.1
	Text msg3b(Point(15,90), "Freshman");
	msg3b.set_color(Color::black);
	Image i1(Point(180,70),"josh.gif");
	msg3.set_color(Color::black);
	Text msg4(Point(15,400), "Timotius Nugroho Chandra - ITB");//Josh B.1
	Text msg4b(Point(15,420),"Fourth Year Student");
	Image i2(Point(180,400),"timotius.gif");
	msg4.set_color(Color::black);
	msg4b.set_color(Color::black);
	Text msg5(Point(400,70), "Raka Mahesa - ITB");//Josh B.1
	Text msg5b(Point(400,90),"Fourth Year Student");
	msg5b.set_color(Color::black);
	Image i3(Point(535,70),"raka.gif");
	msg5.set_color(Color::black);
	
	ab.attach(i1);
	ab.attach(i2);
	ab.attach(i3);
	ab.attach(msg);
	ab.attach(msg2);
	ab.attach(msg3);
	ab.attach(msg3b);
	ab.attach(msg4);
	ab.attach(msg4b);
	ab.attach(msg5);
	ab.attach(msg5b);
	wait_for_menu_bar_click();
	ab.detach(msg);//clean up window
	return;
}

//[RAKA] B3
//Shows help window
void do_help_box(Window& parent)
{
	//Create window
	Window Help(Point(parent.x() + 100, parent.y() + 100), 256, 256, "Help Window");
	Help.callback((Fl_Callback*)Menu_Bar_CB, Address(Close_help_box));
	
	//Create text
	Text HelpMessage(Point(12, 12), "Help:");
	Help.attach(HelpMessage);
	
	//Release later
	wait_for_menu_bar_click();
	Help.detach(HelpMessage);
}

//[RAKA] C
//Shows window asking for input
void do_input_box(Window& parent, string message)
{
	//Create window
	Window NewWindow(Point(parent.x() + 100, parent.y() + 100), 480, 88, "ROMS II");
	NewWindow.callback((Fl_Callback*)Menu_Bar_CB, Address(Close_help_box));
	
	//Create components
	In_box 	Input(Point(100, 12), 360, 24, message);
	Button	Okay(Point(60, 48), 100, 30, "Okay", dialog_okay_cb);
	Button	Cancel(Point(320, 48), 100, 30, "Cancel", dialog_cancel_cb);
	
	//Add to window
	NewWindow.attach(Cancel);
	NewWindow.attach(Input);
	NewWindow.attach(Okay);
	
	//Wait until closed
	wait_for_menu_bar_click();
	
	//Save input
	DialogText 	= Input.get_string();
	DialogValue	= Input.get_int();
	
	//Release
	NewWindow.detach(Cancel);
	NewWindow.detach(Input);
	NewWindow.detach(Okay);
}

//[RAKA] C
//Shows window containing a message
void do_message_box(Window& parent, string message)
{
	//Create window
	Window NewWindow(Point(parent.x() + 100, parent.y() + 100), 320, 48, "ROMS II");
	NewWindow.callback((Fl_Callback*)Menu_Bar_CB, Address(Close_help_box));
	
	//Create text
	Text Message(Point(12, 20), message);
	NewWindow.attach(Message);
	
	//Release later
	wait_for_menu_bar_click();
	NewWindow.detach(Message);
}

//TNC D
//Dispatch to correct graph drawing (Order sales by month; total sales by table; category sales by month)
void draw_graph(Window& parent, ROMS_Menu& menu){
	switch(window_userdata){
	case Graph_order_month:{
		double orderSalesPerMonth[12];

		memset(orderSalesPerMonth, 0.0f, 12*sizeof(double));
		menu.get_sales_order_groupby_month(orderSalesPerMonth);

		for(int i=0; i<12; ++i)
			printf("Order sales for month %d = %.3lf\n", i+1, orderSalesPerMonth[i]);

		do_graph_box(parent, "Order Sales by Month", menu);
		break;
	}
	case Graph_total_table:
		do_graph_box(parent, "Total Sales by Table", menu);
		break;
	case Graph_category_month:
		do_graph_box(parent, "Total Category Sales by Month", menu);
		break;
	}
}

//Create the pop-up window for displaying graph
void do_graph_box(Window& parent, string message, ROMS_Menu& menu){
	//Create the pop-up window
	Window graphWin(Point(parent.x() + 50, parent.y() + 50), 480, 320, message);
	graphWin.callback((Fl_Callback*)Menu_Bar_CB, Address(Close_help_box));
	
	//Initialize
	Graph_lib::Polygon* Bars;	
	/*		int Length		= menu.get_max_table_id() - menu.get_min_table_id() + 1;
			
			stringstream SS;
			SS << "Length " << Length;
			Text Message(Point(12, 20), SS.str());
			graphWin.attach(Message);*/
	
	//[RAKA] D
	//Based on userdata
	switch(window_userdata) {
		case Graph_order_month: {
			break;
		}
		case Graph_total_table: {
			//Get sales
			int Min			= menu.get_min_table_id();
			int Length		= menu.get_max_table_id() - Min + 1 - 30;
			double* Sales 	= menu.get_sales_by_table();
			
			//Draw graph
			Bars = new Graph_lib::Polygon[Length];
			create_bar_graph(graphWin, Sales, Bars, Length, 10, 10, 200, 20, 10);
			
			//[RAKA] NOTE: YOU COULD GET THE TABLE ID BY ITERATING ON THE ARRAY FROM THE MINIMUM ID.
			//  SO IF THE MINIMUM ID IS, SAY, 51, THEN SALES[0] WILL BE FOR TABLE WITH ID 51 + 0 = 51.
			//  LIKE WISE, SALES[100] WILL BE FOR TABLE WITH ID 51 + 100 = 151.
			
			break;
		}
		case Graph_category_month:{
			break;
		}
	}
	
	//Wait for user action
	wait_for_menu_bar_click();
}

//[RAKA] D
void create_bar_graph(Window& parent, double* values, Graph_lib::Polygon* bars, int length, int x, int y, int height, int width, int gap) {
	//Skip if no values
	if (length <= 0) return;

	//Find maximum
	double Max = values[0];
	for (int i = 1; i < length; i++) if (values[i] > Max) Max = values[i];
	
	//For each value
	for (int i = 0; i < length; i++) {
		//Measure rectangle
		int Height 	= (values[i] / Max) * height;//(Values[i] / Max) * (double)height;
		int X 		= x + ((width + gap) * i);
		int Y		= y + (height - Height);
		
		//Create bars
		bars[i].add(Point(X, Y));
		bars[i].add(Point(X + width, Y));
		bars[i].add(Point(X + width, y + height));
		bars[i].add(Point(X, y + height));
		bars[i].set_fill_color(Graph_lib::Color::blue);

		//Attach
		parent.attach(bars[i]);
	}
}

void do_read(Window& w, ROMS_Menu& m, string dfn, string msg, Msg_type type)
{
	//Here again we linked our code directly to the FLTK library instead of creating an interface in our Graph_lib interface to FLTK
	//We did it for lack of time, which reflects the real word. Unfortunately if we wanted to move our code to a different graphics
	//library, we have to change our code and not just our Graphic_lib implementation. we are using the FLTK file chooser directly.
	Fl_File_Chooser fc(dfn.c_str(), "*.dat", Fl_File_Chooser::CREATE, msg.c_str());
	fc.show();
	while (fc.shown())
		Fl::wait();	//wait for OK or Cancel button
	if(0!=fc.value())//make sure there is a string to use as a filename
	{
		//string fname = fc.value();
		stringstream fname;
		fname << fc.value();
		switch(type) {
			case Read_recipes_ingredients:
				m.read_recipes_ingredients(fname);
				break;
			case Read_orders:
				m.Read_orders(fname);
				break;
			case Read_category_menu_items:
				m.read_catprds(fname);
				break;
			default:
				throw InvalidType();
		}
	}
	return;
}
